package com.jzo2o.orders.manager.service.client;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jzo2o.api.customer.AddressBookApi;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class CustomerClient {

    @Resource
    private AddressBookApi addressBookApi;

    @SentinelResource(value = "findAddressBookById",fallback = "findAddressBookByIdFallback",blockHandler = "findAddressBookByIdBlockHandler")
    public AddressBookResDTO findAddressBookById(Long id) {
        log.info("根据id查询地址簿:{}",id);
        return addressBookApi.findById(id);
    }

    public AddressBookResDTO findAddressBookByIdFallback(Long id, Throwable t) {
        log.error("非限流、熔断等导致的异常执行的降级方法，id:{},throwable:", id, t);
        return null;
    }

    public AddressBookResDTO findAddressBookByIdBlockHandler(Long id, BlockException e) {
        log.error("触发限流、熔断时执行的降级方法，id:{},blockException:", id, e);
        return null;
    }
}
